package client

import (
	"5-19yk/common/proto/doctor"
	"context"
	"fmt"
	"google.golang.org/grpc"
)

type Handler func(ctx context.Context, client doctor.DoctorClient) (interface{}, error)

func DoctorClient(ctx context.Context, handler Handler) (interface{}, error) {
	conn, err := grpc.Dial("127.0.0.1:8080", grpc.WithInsecure())
	if err != nil {
		fmt.Printf("连接异常： %s\n", err)
	}
	defer conn.Close()
	// 2. 实例化gRPC客户端
	client := doctor.NewDoctorClient(conn)
	// 3.组装请求参数
	req, _ := handler(ctx, client)
	return req, nil
}
func SendSms(ctx context.Context, req *doctor.SendSmsReq) (*doctor.SendSmsResp, error) {
	client, err := DoctorClient(ctx, func(ctx context.Context, client doctor.DoctorClient) (interface{}, error) {
		sms, err := client.SendSms(ctx, req)
		if err != nil {
			return nil, err
		}
		return sms, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*doctor.SendSmsResp), nil
}
func Login(ctx context.Context, req *doctor.LoginReq) (*doctor.LoginResp, error) {
	client, err := DoctorClient(ctx, func(ctx context.Context, client doctor.DoctorClient) (interface{}, error) {
		sms, err := client.Login(ctx, req)
		if err != nil {
			return nil, err
		}
		return sms, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*doctor.LoginResp), nil
}
func HospitalList(ctx context.Context, req *doctor.HospitalListReq) (*doctor.HospitalListResp, error) {
	client, err := DoctorClient(ctx, func(ctx context.Context, client doctor.DoctorClient) (interface{}, error) {
		sms, err := client.HospitalList(ctx, req)
		if err != nil {
			return nil, err
		}
		return sms, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*doctor.HospitalListResp), nil
}
func DepartmentList(ctx context.Context, req *doctor.DepartmentListReq) (*doctor.DepartmentListResp, error) {
	client, err := DoctorClient(ctx, func(ctx context.Context, client doctor.DoctorClient) (interface{}, error) {
		sms, err := client.DepartmentList(ctx, req)
		if err != nil {
			return nil, err
		}
		return sms, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*doctor.DepartmentListResp), nil
}
func PatientList(ctx context.Context, req *doctor.PatientListReq) (*doctor.PatientListResp, error) {
	client, err := DoctorClient(ctx, func(ctx context.Context, client doctor.DoctorClient) (interface{}, error) {
		sms, err := client.PatientList(ctx, req)
		if err != nil {
			return nil, err
		}
		return sms, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*doctor.PatientListResp), nil
}
func DoctorList(ctx context.Context, req *doctor.DoctorListReq) (*doctor.DoctorListResp, error) {
	client, err := DoctorClient(ctx, func(ctx context.Context, client doctor.DoctorClient) (interface{}, error) {
		sms, err := client.DoctorList(ctx, req)
		if err != nil {
			return nil, err
		}
		return sms, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*doctor.DoctorListResp), nil
}
func OrderCreate(ctx context.Context, req *doctor.OrderCreateReq) (*doctor.OrderCreateResp, error) {
	client, err := DoctorClient(ctx, func(ctx context.Context, client doctor.DoctorClient) (interface{}, error) {
		sms, err := client.OrderCreate(ctx, req)
		if err != nil {
			return nil, err
		}
		return sms, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*doctor.OrderCreateResp), nil
}
func OrderList(ctx context.Context, req *doctor.OrderListReq) (*doctor.OrderListResp, error) {
	client, err := DoctorClient(ctx, func(ctx context.Context, client doctor.DoctorClient) (interface{}, error) {
		sms, err := client.OrderList(ctx, req)
		if err != nil {
			return nil, err
		}
		return sms, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*doctor.OrderListResp), nil
}
